﻿using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZXing.Common;
using ZXing.QrCode;
using ZXing; 

namespace Print.printServer {
    public class Code128Printer {
        String d = "";
        String zhongliang;
        String today;
        String jiaokind;

        public void print(String Code, string zhongliang, string today, string jiaokind) {
            d = Code;
            this.zhongliang = zhongliang;
            this.today= today;
            this.jiaokind= jiaokind;
            PrintDocument pd = new PrintDocument();
            pd.PrintPage += new PrintPageEventHandler(printEventHandler);
          
            pd.DefaultPageSettings.PrinterSettings.PrinterName = "Bar Code Printer T-4503E";
           // pd.DefaultPageSettings.PrinterSettings.PrinterName = "ZDesigner ZD888-203dpi ZPL";
            //打印机名称
            pd .DefaultPageSettings.PaperSize = new PaperSize("Custom", (int)(8000/25.4), (int)(1500/25.4));                                                                                         //pd.DefaultPageSettings.Landscape = true;  //设置横向打印，不设置默认是纵向的
            pd.PrintController = new StandardPrintController();
            pd.Print();
        }



        private void printEventHandler(object sender, PrintPageEventArgs e) { 
           
                Bitmap bmp = new Bitmap(300, 55);
                Graphics g = Graphics.FromImage(bmp);
                Font fntTxt = new Font("宋体",9, FontStyle.Regular);//正文文字    
                Brush brush = new SolidBrush(Color.Black);//画刷           

                g.DrawImage(getbmp(d),120, 20);
                g.DrawImage(CreateQRCode(d ), 1, 1);
                
                g.DrawString(d, fntTxt, brush, new Point(150, 44));
                g.DrawString(jiaokind, fntTxt, brush, new Point(130, 5));
                g.DrawString(zhongliang, fntTxt, brush, new Point(210, 5));

                g.DrawString("计划日期", fntTxt, brush, new Point(55, 3));
                g.DrawString(today, fntTxt, brush, new Point(55, 16));

                g.DrawString("生效日期", fntTxt, brush, new Point(55, 29));
                string SxrqAdd = DateTime.Now.AddDays(50).Date.ToString("yyyy-MM-dd");
                g.DrawString(SxrqAdd, fntTxt, brush, new Point(55, 42));

               
             
            try {

                RectangleF imageBounds = e.MarginBounds;
            
                imageBounds.Width = bmp.Width;
                imageBounds.Height = bmp.Height;
                 

                e.Graphics.DrawImage(bmp, 1,1);
                bmp.Save("aaa.png");
                bmp.Dispose();
            } catch (Exception ee) {
                MessageBox.Show(ee.Message);
            }
        }

        public static Bitmap CreateQRCode(string asset) {
            EncodingOptions options = new QrCodeEncodingOptions {
                DisableECI = true,
                CharacterSet = "UTF-8", //编码
                Width = 60,             //宽度
                Height = 60             //高度
            };
            BarcodeWriter writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            writer.Options = options;
            Bitmap old = writer.Write(asset);

            int margin = 1;
            int w = old.Width - margin * 2;
            int h = old.Height - margin * 2;
            Rectangle cropRect = new Rectangle(margin, margin, w, h);
            Bitmap target = new Bitmap(cropRect.Width, cropRect.Height);
            using (Graphics g = Graphics.FromImage(target)) {
                g.DrawImage(old, new Rectangle(0, 0, target.Width, target.Height), cropRect, GraphicsUnit.Pixel);
            }
            return target;
        }

        private Bitmap getbmp(String code) {
            //设置条码的规格
            EncodingOptions encoding = new EncodingOptions {

                PureBarcode = true,
                Margin = 0,
                Height = 25
            }; 
            //生成条码的图片并保存
            BarcodeWriter bw = new BarcodeWriter();
            //指定规格
            bw.Options = encoding;
            bw.Format = BarcodeFormat.CODE_128;
            
            Bitmap btp = bw.Write(code);
            return btp;
          
        }


      
    }
}
